package com.java.base.exercise.threadpool;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;

/**
 * @Author RenPu
 * @Date 2021/7/3 15:18
 * @Version 1.0
 * @Description: 队列的三种模式：1:有界队列, 2:无界队列，3：同步队列（添加元素的时候，需要有移除元素的操作才会成功）
 **/
public class QueueTest {

    @Test
    public void queueOne() throws InterruptedException {

        //有界队列
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(10);

        for (int i = 0; i < 20; i++) {
            arrayBlockingQueue.put(i);
            System.out.println("向对列添加的值为" + i);
        }

    }


    //可以为有界队列，也可以为无界队列
    @Test
    public void queueTwo() throws InterruptedException {

        //当参数不设置时为无界队列，当参数设置时为有界队列
        LinkedBlockingDeque<Object> objects = new LinkedBlockingDeque<>();

        for (int i = 0; i < 20; i++) {
            objects.put(i);
            System.out.println("向对列添加的值为" + i);
        }

    }


    //同步队列（类似mq）
    @Test
    public void queueThree() throws InterruptedException {

        SynchronousQueue<Object> objects = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                objects.put(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("插入成功！！！");
        }).start();


//        new Thread(()->{
//            try {
//                objects.take();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("删除成功！！！");
//        }).start();


        Thread.sleep(100 * 100);


    }


}
